//
//  ChangePassWordViewController.m
//  XinQiCheng
//
//  Created by 伯纳德 on 17/4/11.
//  Copyright © 2017年 Richard. All rights reserved.
//

#import "ChangePassWordViewController.h"
#import "AppDelegate.h"
#import "LoginController1.h"
#import "iToast.h"
#import "WebServices.h"

@interface ChangePassWordViewController ()

@property (nonatomic ,strong)UIButton *sureBut;
@property (nonatomic ,strong)UITextField *oldPassWordTextField;
@property (nonatomic ,strong)UITextField *passWordOneTextField;
@property (nonatomic ,strong)UITextField *passWordTwoTextField;

@end

@implementation ChangePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //背景图
    UIImageView *bgIV = [[UIImageView alloc]initWithFrame:self.view.frame];
    bgIV.image = [UIImage imageNamed:@"mainbg"];
    [self.view addSubview:bgIV];
    
    //返回按钮
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 30, 34, 34)];
    [backBtn setImage:[UIImage imageNamed:@"changpwd_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, [UIScreen mainScreen].bounds.size.width, 34)];
    titleLabel.text = @"修改密码";
    titleLabel.textColor = [UIColor colorWithRGBColorByString:@"#c2e6f9"];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:titleLabel];
    
    //线
    UIView *lineView_w = [[UIView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 0.3)];
    lineView_w.backgroundColor = [UIColor colorWithRed:(237)/255.0 green:(237)/255.0 blue:(87)/255.0 alpha:0.5];
    [self.view addSubview:lineView_w];

    //白色背景
    UIView *bgBigV = [[UIView alloc]initWithFrame:CGRectMake(12, 64, [UIScreen mainScreen].bounds.size.width-24, 220)];
    bgBigV.backgroundColor = [UIColor clearColor];
    bgBigV.layer.shadowOpacity = 0.5;
//    bgBigV.layer.shadowColor = MRRGBColor(66, 183, 239).CGColor;
    bgBigV.layer.shadowRadius = 4;
    bgBigV.layer.shadowOffset = CGSizeMake(4, 4);
    bgBigV.layer.cornerRadius = 6;
    [self.view addSubview:bgBigV];
    
    //设置密码指示图
    UIImageView *phoneIV1 = [[UIImageView alloc]initWithFrame:CGRectMake(25, 35, 15, 15)];
    phoneIV1.image = [UIImage imageNamed:@"change_psw-2"];
    [bgBigV addSubview:phoneIV1];
    
    //设置密码输入框
    self.oldPassWordTextField = [[UITextField alloc] initWithFrame:CGRectMake(52, 25, bgBigV.bounds.size.width - 104, 40)];
    self.oldPassWordTextField.keyboardType = UIKeyboardTypeDefault;
    self.oldPassWordTextField.placeholder = @"原密码";
//    [self.oldPassWordTextField setValue:[UIColor colorWithRGBColorByString:@"#86a0b4"] forKeyPath:@"_placeholderLabe.textColor"];
    self.oldPassWordTextField.secureTextEntry = YES;
    self.oldPassWordTextField.textColor = [UIColor whiteColor];
    self.oldPassWordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.oldPassWordTextField.font = [UIFont systemFontOfSize:14];
    [bgBigV addSubview:self.oldPassWordTextField];
    
    //线
    UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(25, 65, bgBigV.bounds.size.width - 50, 0.5)];
    lineView1.backgroundColor = [UIColor colorWithRed:(237)/255.0 green:(237)/255.0 blue:(237)/255.0 alpha:0.5];
     lineView_w.alpha = 0.5;
    [bgBigV addSubview:lineView1];
    
    //设置密码指示图
    UIImageView *phoneIV2 = [[UIImageView alloc]initWithFrame:CGRectMake(25, 100, 15, 15)];
    phoneIV2.image = [UIImage imageNamed:@"change_psw-2"];
    [bgBigV addSubview:phoneIV2];
    
    //设置密码输入框
    self.passWordOneTextField = [[UITextField alloc] initWithFrame:CGRectMake(52, 90, bgBigV.bounds.size.width - 104, 40)];
    self.passWordOneTextField.keyboardType = UIKeyboardTypeDefault;
//    [self.passWordOneTextField setValue:[UIColor colorWithRGBColorByString:@"#86a0b4"] forKeyPath:@"_placeholderLabel.textColor"];
    self.passWordOneTextField.placeholder = @"新密码";
    self.passWordOneTextField.secureTextEntry = YES;
    self.passWordOneTextField.textColor = [UIColor whiteColor];
    self.passWordOneTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.passWordOneTextField.font = [UIFont systemFontOfSize:14];
    [bgBigV addSubview:self.passWordOneTextField];
    
    //线
    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(25, 130, bgBigV.bounds.size.width - 50, 0.5)];
    lineView2.backgroundColor = [UIColor colorWithRed:(237)/255.0 green:(237)/255.0 blue:(237)/255.0 alpha:0.5];
    [bgBigV addSubview:lineView2];
    
    //设置密码指示图
    UIImageView *phoneIV3 = [[UIImageView alloc]initWithFrame:CGRectMake(25, 160, 15, 15)];
    phoneIV3.image = [UIImage imageNamed:@"change_psw-2"];
    [bgBigV addSubview:phoneIV3];
    
    //设置密码输入框
    self.passWordTwoTextField = [[UITextField alloc] initWithFrame:CGRectMake(52, 150, bgBigV.bounds.size.width - 104, 40)];
    self.passWordTwoTextField.keyboardType = UIKeyboardTypeDefault;
//    [self.passWordTwoTextField setValue:[UIColor colorWithRGBColorByString:@"#86a0b4"] forKeyPath:@"_placeholderLabel.textColor"];
    self.passWordTwoTextField.placeholder = @"确认密码";
    self.passWordTwoTextField.secureTextEntry = YES;
    self.passWordTwoTextField.textColor = [UIColor whiteColor];
    self.passWordTwoTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.passWordTwoTextField.font = [UIFont systemFontOfSize:14];
    [bgBigV addSubview:self.passWordTwoTextField];
    
    //线
    UIView *lineView3 = [[UIView alloc]initWithFrame:CGRectMake(25, 190, bgBigV.bounds.size.width - 50, 0.5)];
    lineView3.backgroundColor = [UIColor colorWithRed:(237)/255.0 green:(237)/255.0 blue:(237)/255.0 alpha:0.5];
    [bgBigV addSubview:lineView3];
    
    //确定按钮
    self.sureBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sureBut setTitle:@"确定" forState:UIControlStateNormal];
    self.sureBut.backgroundColor =[UIColor colorWithRGBColorByString:@"#17174c"];
    self.sureBut.font =[UIFont systemFontOfSize:15];
    self.sureBut.layer.cornerRadius = 20;
    self.sureBut.layer.borderColor = [UIColor colorWithRGBColorByString:@"#7ecef4"];
    [self.sureBut.layer setBorderWidth:1];
    [self.sureBut setTitleColor:[UIColor colorWithRGBColorByString:@"#c2e6f9"] forState:UIControlStateNormal];
    [self.sureBut setFrame:CGRectMake(37, CGRectGetMaxY(bgBigV.frame), [UIScreen mainScreen].bounds.size.width - 74, 40)];
    [self.sureBut addTarget:self action:@selector(sureButClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sureBut];
    
     //收键盘
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    [self.view addGestureRecognizer:singleTap];
    
}

//收键盘
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer{
    
    [self.view endEditing:YES];
    
}

//返回按钮
-(void)backBtnClick{
    
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//确定按钮
- (void)sureButClick{
    
    [self.view endEditing:YES];
    
    if (self.passWordOneTextField.text.length == 0 || self.passWordOneTextField.text.length == 0 || self.passWordTwoTextField.text.length == 0) {

        NSString *nameError = NSLocalizedString(@"请先输入信息", nil);
        [iToast showTip:nameError gravity:iToastGravityCenter];
        
        return;
        
    }
    
    if (![self.passWordOneTextField.text isEqualToString:self.passWordTwoTextField.text]) {
    
        NSString *nameError = NSLocalizedString(@"两次输入密码不一致", nil);
        [iToast showTip:nameError gravity:iToastGravityCenter];
        
        return;
    }
    
    //修改密码
    [self changePWD];
    
}

//修改密码
-(void)changePWD{
    
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"sessionId\":\"%@\",\"oldPasswd\":\"%@\",\"newPasswd1\":\"%@\",\"newPasswd2\":\"%@\"}]";
    NSLog(@"changePWD____fomat = %@",fomat);
    NSString *body = [NSString stringWithFormat:fomat, sessionId,self.oldPassWordTextField.text,self.passWordOneTextField.text,self.passWordTwoTextField.text];
    [WebServices requestStartOrder:@"changePassword" body:body target:self action:@selector(changePWD:) errorTip:nil];
   
}

- (void)changePWD:(NSDictionary *)dic{
    
    NSLog(@"changePWD____%@", dic);
    NSString *result = [NSString stringWithFormat:@"%@",[dic objectForKey:@"result"]];
    if ([result isEqualToString:@"0"]) {
        
        UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        LoginController1 *newT = [[LoginController1 alloc] init];
        window.rootViewController = newT;
        [window makeKeyAndVisible];

    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
